<template>
  <div>
    <!-- 头部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div class="topbox">
        <el-input
          placeholder="请输入内容"
          clearable
          v-model="querinfo.query"
          @clear="getgoodslist()"
        >
          <el-button
            class="btn"
            slot="append"
            icon="el-icon-search"
            @click="getgoodslist()"
          ></el-button>
        </el-input>
        <el-button type="primary" class="addgoods" @click="addgoods"
          >添加商品</el-button
        >
      </div>
      <el-table :data="listdata" border stripe>
        <el-table-column type="index" label="##"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="100px"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="100px"
        ></el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品数量"
          width="100px"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="170px">
          <template v-slot="scope">
            {{ scope.row.add_time | timefilter }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="setgoods(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="delgoods(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querinfo.pagenum"
        :page-sizes="[8, 10, 12]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
      <!-- 编辑按钮对话框 -->
      <el-dialog title="提示" :visible.sync="setgoodsisblock" width="50%">
        <el-form ref="setgoodsfrom" :model="setgoodsfrom" label-width="80px">
          <el-form-item label="商品名称">
            <el-input v-model="setgoodsfrom.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input v-model="setgoodsfrom.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品数量">
            <el-input v-model="setgoodsfrom.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品重量">
            <el-input v-model="setgoodsfrom.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setgoodsisblock = false">取 消</el-button>
          <el-button type="primary" @click="quersendset">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //获取商品列表的查询信息
      querinfo: {
        query: "",
        pagenum: 1,
        pagesize: 8,
      },
      //商品列表数据
      listdata: [],
      //商品的总数
      total: 0,
      //编辑商品的对话框是否显示
      setgoodsisblock: false,
      setgoodsfrom: {},
    };
  },
  created() {
    this.getgoodslist();
  },
  methods: {
    //获取商品列表
    async getgoodslist() {
      const { data: res } = await this.$http.get("goods", {
        params: this.querinfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品信息失败");
      this.listdata = res.data.goods;
      this.total = res.data.total;
    },
    //每页展示的商品数量发生改变的事件
    handleSizeChange(size) {
      this.querinfo.pagesize = size;
      this.getgoodslist();
    },
    //页码发生改变的事件
    handleCurrentChange(num) {
      this.querinfo.pagenum = num;
      this.getgoodslist();
    },
    //删除商品
    async delgoods(row) {
      const msg = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (msg !== "confirm") return this.$message.error("用户取消删除");
      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`);
      if (res.meta.status !== 200) return this.$message.error("删除失败");
      this.$message.success("删除成功");
      this.getgoodslist();
    },
    //添加商品
    addgoods() {
      this.$router.push("/goods/add");
    },
    //修改按钮
    setgoods(row) {
      console.log(row);
      this.setgoodsfrom = {
        goods_name: row.goods_name,
        goods_price: row.goods_price,
        goods_weight: row.goods_weight,
        goods_number: row.goods_name,
        goods_id: row.goods_id,
        goods_cat: "0",
      };
      this.setgoodsisblock = true;
    },
    //确认修改
    async quersendset() {
      const { data: res } = await this.$http.put(
        `goods/${this.setgoodsfrom.goods_id}`,
        this.setgoodsfrom
      );
      if (res.meta.status != 200) return this.$message.error("修改失败");
      this.$message.error("修改成功");
    },
  },
};
</script>

<style scoped>
.el-card {
  margin: 20px 0;
}
.topbox {
  width: 450px;
  display: flex;
  justify-content: space-between;
}
.btn {
  width: 70px;
}
.addgoods {
  margin: 0 30px;
}
</style>